﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZYStudios.Models;
using DoubanApi.Models;
using ZYStudios.Common;
using ZYStudios.Filter;
using ZYStudios.Helper;
using System.Net;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace ZYStudios.Controllers
{
    [DataForMasterPageFilter]
    [UserAuthFilter]
    public class MovieController : BaseController
    {
        //
        // GET: /Movie/

        [OutputCache(Duration = int.MaxValue, VaryByParam = "*")]
        public ActionResult Index()
        {
            var aMovieCachaProvider = CreateFileCache();
            DoubanApi.DoubanApiClient apiclient = CreateDoubanApi();
            List<MovieModel> aNewMovieList = new List<MovieModel>();


            ZYStudios.Models.Data aData = new Models.Data();
            ZYStudios.Models.DataTableAdapters.NewMovieTableAdapter aNewMovieAdapter = new Models.DataTableAdapters.NewMovieTableAdapter();
            aNewMovieAdapter.Fill(aData.NewMovie);



            foreach (var aNewMovieRow in aData.NewMovie)
            {
                var aMovieCache = aMovieCachaProvider.Get(aNewMovieRow.MovieID) as MovieModel;
                if (aMovieCache == null)
                {
                    aMovieCache = apiclient.GetMovie(int.Parse(aNewMovieRow.MovieID));
                    aMovieCachaProvider.Add(aMovieCache.Id.ToString(), aMovieCache, DateTime.Now.AddDays(2));
                }
                aMovieCache.Title = aNewMovieRow.MovieName;
                aNewMovieList.Add(aMovieCache);
            }

            ViewData["NewMovies"] = aNewMovieList;
            return View();
        }


        [OutputCache(Duration = 3600*24*2, VaryByParam = "*")]
        public ActionResult Movie(string id)
        {
            bool aIsFind = true;
            var aMovieCachaProvider = CreateFileCache();
            var aMovieCache = aMovieCachaProvider.Get(id.ToString()) as MovieModel;
            if (aMovieCache == null)
            {
                aMovieCache = aMovieCachaProvider.Get(id.GetHashCode().ToString()) as MovieModel;
                if (aMovieCache == null)
                {
                    int aMovieId;
                    if (int.TryParse(id, out aMovieId))
                    {
                        try
                        {
                            DoubanApi.DoubanApiClient apiclient = CreateDoubanApi();
                            aMovieCache = apiclient.GetMovie(aMovieId);
                            aMovieCachaProvider.Add(aMovieCache.Id.ToString(), aMovieCache, DateTime.Now.AddDays(2));
                            aMovieCachaProvider.Add(aMovieCache.Title.GetHashCode().ToString(), aMovieCache, DateTime.Now.AddDays(2));
                        }
                        catch (Exception)
                        {
                           aIsFind = false;
                        }
                    }
                    else
                    {
                        aIsFind = false;
                    }
                }
            }
            if (aIsFind)
            {
                ViewData["Movie"] = aMovieCache;
                return View("Movie");
            }
            else
            {
                return RedirectToAction("NotFound", "Default");
            }
        }

        [OutputCache(Duration = int.MaxValue, VaryByParam = "*")]
        public ActionResult Playtime()
        {
            try
            {

                List<PlaytimeModel> data = PlaytimeHelper.ReadPlayTimeData();


                int select = 0;
                ViewData["PlayLineSelect"] = select;
                var aShowPlayLine = new List<PlaytimeModel>();
                foreach (var item in data)
                {
                    DateTime playdate = new Chinese2Number().ConvertTime(item.PlayData);
                    if (DateTime.Parse(playdate.Date.ToShortDateString()) >= DateTime.Parse(DateTime.Now.Date.ToShortDateString()))
                    {
                        if (playdate.Year == DateTime.Now.Year && playdate.Month == DateTime.Now.Month && playdate.Day == DateTime.Now.Day)
                        {
                            ViewData["PlayLineSelect"] = select;
                        }
                        aShowPlayLine.Add(item);
                        select++;
                    }
                }
                ViewData["PlayLine"] = aShowPlayLine.Take(7).ToList();
            }
            catch (Exception ex)
            {
                ViewData["PlayLine"] = new List<PlaytimeModel>();
            }

            return View();
        }



    }
}
